﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using CommandPrompt;


namespace Bacon_FTP_Client
{
    /// <summary>
    /// Static class that contains methods for procesing and validating commands for the FTP server
    /// </summary>
    public static class CommandProcesing
    {
        #region Implementation()
        /// <summary>
        /// Commands validation for the client
        /// </summary>
        /// <param name="commandToSent">String of the command for the server</param>
        /// <param name="parametersSent">String of the parameter(s) for the server</param>
        /// <param name="clientServices">ClientServices instance</param>
        /// <returns>Returns the response from the server.</returns>
        public static string[] CommandValidation(string commandToSend, string[] parametersSent, ClientServicesST clientServices, Client client)
        {
            string[] message = {"", ""};
            CommandsDictionary dictionary = new CommandsDictionary();

            string command = null;

            if (commandToSend.Length > 4)
                command = commandToSend.Remove(4);
            else
                command = commandToSend;            

            try
            {
                if (command == Constants.storCmd)
                {
                    message[0] = clientServices.SendPASVCommand();

                    message[1] = clientServices.AttempToSendFile(Path.GetFileName(commandToSend.Remove(0, 4)),
                        Path.GetFullPath(commandToSend.Remove(0, 4)), message[0], client);

                    return message;
                }
                else if (command == Constants.retrCmd)
                {                    
                    message[0] = clientServices.SendPASVCommand();

                    message[1] = clientServices.AttempToReceiveFile(Path.GetFileName(commandToSend.Remove(0, 4)),
                        Path.GetFullPath(commandToSend.Remove(0, 4)), message[0], client);
                    
                    return message; 
                }
                else if (command == Constants.listCmd)
                {
                    message[0] = clientServices.SendPASVCommand();

                    message[1] = clientServices.AttempToList(command, message[0], client);
                }

                message[0] = clientServices.Comunicate(commandToSend);

                return message;
            }
            catch (KeyNotFoundException)
            {
                return message;
            }
        }

        /// <summary>
        /// Method for client autentication.
        /// </summary>
        /// <param name="clientServices">Instance of the ClientServices</param>
        /// <param name="client">Instance of the Client</param>
        /// <param name="step">Steps to autenticate {welcomeMsg, Username, Password}</param>
        /// <returns>Returns the response from the server</returns>
        public static string Autenticate(ClientServicesST clientServices, Client client, string step)
        {
            string message;

            if (step == "WelcomeMsg")
            {
                message = clientServices.Comunicate(null);
            }
            else if (step == "Username")
            {
                message = clientServices.Comunicate(Constants.userCmd + " " + client._username);
            }
            else
            {
                message = clientServices.Comunicate(Constants.passCmd + " " + client._password);
            }

            return message;
        }

        #endregion
    }
}
